<include file="../header" />
</head>
<body class="J_scroll_fixed">
<script type="text/javascript">

    var ids = new Array();
    var rowNum = 0;
    function onlyNumber(obj){
        //得到第一个字符是否为负号
        var t = obj.value.charAt(0);
        //先把非数字的都替换掉，除了数字和.
        if(obj.value != 0)
        obj.value = obj.value.replace(/[^\d\.]/g,'').replace(/^0{1,}/g,'');
        //必须保证第一个为数字而不是.
        obj.value = obj.value.replace(/^\./g,'');
        //保证只有出现一个.而没有多个.
        obj.value = obj.value.replace(/\.{2,}/g,'.');
        //保证.只出现一次，而不能出现两次以上
        obj.value = obj.value.replace('.','$#$').replace(/\./g,'').replace('$#$','.');
        //只能输入两位小数
        obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3');
        //如果第一位是负号，则允许添加
        if(t == '-'){
            return;
        }

        var index = obj.name.substring(obj.name.indexOf("_") + 1, obj.name.length);
        var numberName = "number_" + index;
        var number = parseFloat($("input[name="+numberName+"]").val());
        var storageNumber = parseFloat($("input[name=storage_number_"+index+"]").val());
        if(number > storageNumber){
            $("input[name="+numberName+"]").val(storageNumber);
            number = storageNumber;
        }
        var priceName = "price_" + index;
        var price = parseFloat($("input[name="+priceName+"]").val());
        var totalPriceName = "total_price_" + index;
        $("input[name="+totalPriceName+"]").val(price*number);
    }

    function insertRows(){
        //获取表格对象
        var tb1 = $("#viewTabs");                // 找table的ID
//        var rowNum= $("#viewTabs tr").size();  //获取表格的行数
        //var liNum=$("tr[id^='_tr_']").size();  //获取以_tr_开头测tr个数

        var code = $("select[name=codeId]").val();
        var codeArr = code.split(",");
        var codeId = codeArr[0];
        var materialName = codeArr[1];
        var spec = codeArr[2];
        var prop = codeArr[3];
        var unit = codeArr[4];
        var storage = $("select[name=batchNo]").val();
        var storageArr = storage.split(",");
        var storageId = storageArr[0];
        var batchNo = storageArr[1];
        var price = storageArr[2];
        var storageNumber = storageArr[3];
        var supplierId = storageArr[4];
        var codeName = $("select[name=codeId]").find("option:selected").text();

        var serial = materialName +"（" +batchNo +"）";
        if ($.inArray(serial, ids) >= 0) {
            alert("已添加过【" + serial + "】，请直接修改");
            return;
        }else{
            ids.push(serial);
        }

        var $td0 = $("<td></td>"); //创建第一个td
        $td0.html(codeName+"<input type=\'hidden\' class=\'input\' name=\'code_id_"+rowNum+"\' value=\'"+codeId+"\'/>"); //html把序号放到了第一个td里面

        var $td1 = $("<td></td>");
        $td1.html(materialName);

        var $td2 = $("<td></td>");
        $td2.html(spec);

        var $td3 = $("<td></td>");
        $td3.html(prop);

        var $td4 = $("<td></td>");
        $td4.html("<input type=\'text\' class=\'input batchNo\' name=\'batch_no_"+rowNum+"\' value=\'"+batchNo+"\' readonly/>"+"<input type=\'hidden\' class=\'input\' name=\'storage_id_"+rowNum+"\' value=\'"+storageId+"\'/>"+"<input type=\'hidden\' class=\'input\' name=\'supplier_id_"+rowNum+"\' value=\'"+supplierId+"\'/>");

        var $td5 = $("<td></td>");
        $td5.html("<input type=\'text\' class=\'input number\' name=\'number_"+rowNum+"\' onkeyup=\'onlyNumber(this)\' value='1'/>");

        var $td6 = $("<td></td>");
        $td6.html(unit);

        var $td7 = $("<td></td>");
        $td7.html("<input type=\'text\' class=\'input price\' name=\'price_"+rowNum+"\' value='" + price +"' readonly/>");

        var $td8 = $("<td></td>");
        $td8.html("<input type=\'text\' class=\'input number\' name=\'storage_number_"+rowNum+"\' value='" + storageNumber +"' readonly/>");

        var $td9 = $("<td></td>");
        $td9.html("<input type=\'text\' class=\'input totalPrice\' name=\'total_price_"+rowNum+"\' readonly value='"+ price +"'/>");

        var $td10 = $("<td></td>");
        $td10.html("<a class=\'btn btn-danger btn-del\'> 移除</a>");

        var $tr = $("<tr id=\'_tr_"+rowNum+"\' class=\'in-opt\'></tr>");// 创建tr，将3个td放置到tr中
        $tr.append($td0);
        $tr.append($td1);
        $tr.append($td2);
        $tr.append($td3);
        $tr.append($td4);
        $tr.append($td5);
        $tr.append($td6);
        $tr.append($td7);
        $tr.append($td8);
        $tr.append($td9);
        $tr.append($td10);

        //在表格的最后追加新增的tr
        tb1.append($tr);

        var dateInput = $("input.J_date1")
        if (dateInput.length) {
            Wind.use('datePicker', function () {
                dateInput.datePicker();
            });
        }

//        $("select[name='codeId']").find("option").eq(0).prop("selected",true);
        $("select[name='batchNo']").find("option").eq(0).prop("selected",true);
        $(".addRow").hide();
//        var row = $("input[name=row]").val();
        rowNum++; //用于新增tr、td、属性下标
        $("input[name=row]").val(rowNum);
    }

    $(function() {

        $(".search").click(function() {
//            $("select[name=codeId]").show();
            var storeId = $("select[name='store_id']").val();
            $(".addRow").hide();
            $(".batchNoList").hide();
            if(storeId  == 0){
                alert("请先选择【发料仓】");
                $("select[name='store_id']").focus();
                $("select[name='codeId']").find("option").eq(0).prop("selected",true);
                return false;
            }
            $("select[name=codeId]").empty();
            $("select[name=codeId]").append("<option value='0'></option>");
            var codeName = $("input[name=codeName]").val().toLowerCase();
            var listArr=<?php echo json_encode($listCode);?>;
            var list=eval(listArr);
            var count = 0;
            for(var i=0;i<list.length;i++){
                if(list[i]["name"].toLowerCase().indexOf(codeName) != -1){
                    var val = list[i]["code_id"] + "," + list[i]["material_name"] + "," + list[i]["spec"] + "," + list[i]["prop"] + "," + list[i]["unit"] + "," + list[i]["barcode"];
                    $("select[name=codeId]").append("<option value='"+val+"'>"+list[i]["parent_name"]+"."+list[i]["code_name"]+"</option>");
                    count++;
                }
            }
            if(count>0){
                $(".codeList").show();
                $("select[name=codeId]").focus();
            }else{
                $(".codeList").hide();
            }
        });

        $("button.btn_submit").click(function() {

           if($("#viewTabs tr").size() == 1){
                alert("请增加领料单");
                $("input[name=codeName]").focus();
                return false;
            }

            if ($("select[name=store_id]").val() == 0) {
                alert("请选择【发料仓】");
                $("select[name=store_id]").focus();
                return false;
            }

            if (parseInt("{$workorder.claim_status}") > -2){
               if ($("input[name=claim_audit_date]").val() == 0) {
                    alert("请选择【审核时间】");
                    $("input[name=claim_audit_date]").focus();
                    return false;
                }
            }else{
                if ($("input[name=claim_date]").val() == 0) {
                    alert("请选择【领料时间】");
                    $("input[name=claim_date]").focus();
                    return false;
                }
            }


            if (confirm("提交采购入库将无法再次修改，是否继续提交")) {
                $("button.btn_submit").hide();
            } else {
                return false;
            }

        });

        $("select[name='codeId']").change(function () {
            var storeId = $("select[name='store_id']").val();
            $(".addRow").hide();
            if(storeId  == 0){
                alert("请先选择【发料仓】");
                $("select[name='store_id']").focus();
                $("select[name='codeId']").find("option").eq(0).prop("selected",true);
                return false;
            }

//            $("select[name=batchNo]").show();
            $("select[name=batchNo]").empty();
            $("select[name=batchNo]").append("<option value='0'></option>");
            var codeName = $("select[name='codeId']").val();
            var codeId = codeName.substring(0, codeName.indexOf(","));
            var storageArr=<?php echo json_encode($listStorage);?>;
            var count = 0;
            var list=eval(storageArr);
            for(var i=0;i<list.length;i++){
                if(list[i]["code_id"] == codeId && list[i]["store_id"] == storeId){
                    var val = list[i]["storage_id"] + "," + list[i]["batch_no"] + "," + list[i]["price"]+ "," + list[i]["storage_number"]+ "," + list[i]["supplier_id"];
                    $("select[name=batchNo]").append("<option value='"+val+"'>"+list[i]["batch_no"]+" (剩余 "+list[i]["storage_number"]+" "+list[i]["unit"]+"，"+list[i]["production_date"] +"~" + list[i]["expiry_date"] +"）</option>");
                    count++;
                }
            }
            if(count>0){
                $(".batchNoList").show();
                $("select[name=batchNo]").focus();
            }else{
                $(".batchNoList").hide();
            }
        });

        $("select[name='batchNo']").change(function () {
            if($("select[name='batchNo']").val() != 0){
                $(".addRow").show();
            }else{
                $(".addRow").hide();
            }
        });

        var listArr=<?php echo json_encode($list);?>;
        var list=eval(listArr);
        if(list != null){
            rowNum = list.length;
            for(var i=0;i<list.length;i++){
                var serial = list[i]["material_name"] +"（" +list[i]["batch_no"] +"）";
                ids.push(serial);
            }
        }else{
            rowNum = 0;
        }

        //移除
        $("#viewTabs").on('click', '.btn-del', function () {
            var materialName = $(this).closest('tr').children('td').eq(1).html();
            var batchNo = $(this).closest('tr').children('td').eq(4).children('input').eq(0).val();
            var serial = materialName +"（" +batchNo +"）";
            ids.splice($.inArray(serial,ids),1);
            $(this).closest('tr').remove();
        });

        $("input[name=unAudit]").click(function() {
            if($("input[name=unAudit]").is(':checked')) {
                $("button.btn_submit").text("拒绝");
                $("input[name=btnText]").val("拒绝");
            }else{
                $("button.btn_submit").text("审核");
                $("input[name=btnText]").val("审核");
            }
        });
    });

</script>
<style type="text/css">
    select[readonly] {
        background: #eee;
        cursor: no-drop;
    }

    select[readonly] option {
        display: none;
    }
    .btn-danger,.btn-info,.btn-success,.btn-warning{
        width: 50px;
        /*height: 18px;*/
        padding: 2px 0px 2px 0px;
        margin: 1px 0px 1px 0px;
    }
    .code{
        width: 150px;
    }
    .batchNo{
        width: 150px;
    }
    .number{
        width: 40px;
    }
    .price{
        width: 70px;
    }
    .J_date{
        width: 100px;
    }
    .totalPrice{
        width: 70px;
    }
    .img-process{
        width: 70px;
        height: 70px;
        /*cursor: pointer;*/
    }
</style>
<form class="well form-search"  style="float:left;width:100%;margin-bottom: -20px" >
    <a style="color:#45a1de;font-weight: bold;font-size: large">领料单</a>
    &nbsp;&nbsp;&nbsp;
    <if condition="$workorder['claim_status'] gt '-1'">
        <a style="color: #1cc385;font-weight: bold;font-size: large">（已审核）</a>
        <elseif condition="$workorder['claim_status'] eq -2"/>
        <a style="color: #ed7667;font-weight: bold;font-size: large">（已反审核）</a>
        <else/>
        <a style="color: #ed7667;font-weight: bold;font-size: large">（未审核）</a>
    </if>
    <br/><br/>
    <php>$imgVersion = "2020051401";</php>
    <div class="search_type cc mb10">
        <div >
            <span class="mr20">
                工单号：<input type="text" value="{$workorder.workorder_no}" readonly/>&nbsp;
                患者：<input type="text" value="{$patient.patient_name}，{$patient.sex}，{$patient.age}岁 {$workorder.side}" readonly/>&nbsp;
                业务员：<input type="text" style="width: 110px" value="{$sales.admin_name}" readonly/>&nbsp;
                处方人：<input type="text" style="width: 110px" value="{$prescriber.admin_name}" readonly/>
            </span>
        </div>
        <br/>
        <php>
            $processStatusArr=explode(",",$workorder["process_status"]);
            if(in_array($workorder["workorder_type"],array('1','5','6','7')))
            $processArr = array("取阴模", "检查取阴模","修阳模","检查修阳模","热塑成型","检查热塑成型","打磨抛光","检查打磨抛光","组装对线","半成品适配","穿戴及使用训练","入库");
            if(in_array($workorder["workorder_type"],array('2','4')))
            $processArr = array("取阴模2", "检查取阴模2","修阳模2","检查修阳模2","热塑成型","检查热塑成型","打磨抛光","检查打磨抛光","组装对线","半成品适配2","穿戴及使用训练2","入库");
            for($i = 0; $i < 12; $i++){
            if($processStatusArr[$i] == "1"){
            $processArr[$i] .= "亮";
            }
            }
        </php>
        <td>
            工序：
            <if condition="$orthosisProcess[0] eq '1'">
                <img class="img-process" id="process1" src="/Template/images/{$processArr.0}.png?v={$imgVersion}" title="取阴模"/>
            </if>
            <if condition="$orthosisProcess[1] eq '1'">
                <img class="img-process" id="process2" src="/Template/images/{$processArr.1}.png?v={$imgVersion}" title="检查取阴模"/>
            </if>
            <if condition="$orthosisProcess[2] eq '1'">
                <img class="img-process" id="process3" src="/Template/images/{$processArr.2}.png?v={$imgVersion}" title="修阳模"/>
            </if>
            <if condition="$orthosisProcess[3] eq '1'">
                <img class="img-process" id="process4" src="/Template/images/{$processArr.3}.png?v={$imgVersion}" title="检查修阳模"/>
            </if>
            <if condition="$orthosisProcess[4] eq '1'">
                <img class="img-process" id="process5" src="/Template/images/{$processArr.4}.png?v={$imgVersion}" title="热塑成型"/>
            </if>
            <if condition="$orthosisProcess[5] eq '1'">
                <img class="img-process" id="process6" src="/Template/images/{$processArr.5}.png?v={$imgVersion}" title="检查热塑成型"/>
            </if>
            <if condition="$orthosisProcess[6] eq '1'">
                <img class="img-process" id="process7" src="/Template/images/{$processArr.6}.png?v={$imgVersion}" title="打磨抛光"/>
            </if>
            <if condition="$orthosisProcess[7] eq '1'">
                <img class="img-process" id="process8" src="/Template/images/{$processArr.7}.png?v={$imgVersion}" title="检查打磨抛光"/>
            </if>
            <if condition="$orthosisProcess[8] eq '1'">
                <img class="img-process" id="process9" src="/Template/images/{$processArr.8}.png?v={$imgVersion}" title="组装对线"/>
            </if>
            <if condition="$orthosisProcess[9] eq '1'">
                <img class="img-process" id="process10" src="/Template/images/{$processArr.9}.png?v={$imgVersion}" title="半成品适配"/>
            </if>
            <if condition="$orthosisProcess[10] eq '1'">
                <img class="img-process" id="process11" src="/Template/images/{$processArr.10}.png?v={$imgVersion}" title="穿戴及使用训练"/>
            </if>
            <if condition="$orthosisProcess[11] eq '1'">
                <img class="img-process" id="process12" src="/Template/images/{$processArr.11}.png?v={$imgVersion}" title="入库"/>
            </if>
        </td>
    </div>
    <hr class="half-rule">
</form>
<div class="wrap jj">
    <div class="common-form">
        <form method="post" class="form-horizontal J_ajaxForm" action="{:U('Workorder/claimMaterialPost')}">
            <fieldset>
                <div class="control-group">
                    发料仓：
                    <select name="store_id">
                        {:getCommonSelectOptionHtml($workorder['material_store_id'], "Store", "store_id", "store_name", null, $user['enterprise_code'], 1)}
                    </select>
                    &nbsp;&nbsp;&nbsp;&nbsp;
                    备注：
                    <input type="text" class="input" style="width: 790px" name="remark" value="{$workorder.material_remark}">
                </div>
                <hr class="half-rule">
                <table class="table table-hover table-bordered" id="viewTabs">
                    <thead>
                        <tr>
                            <th style="min-width: 80px; width: 80px">编码</th>
                            <th>名称</th>
                            <th style="min-width: 50px;width: 200px">规格型号</th>
                            <th style="min-width: 30px;width: 70px">辅助属性</th>
                            <th style="min-width: 30px; width: 50px">批号</th>
                            <th style="min-width: 30px; width: 60px">数量</th>
                            <th style="min-width: 30px; width: 50px">单位</th>
                            <th style="min-width: 40px; width: 70px">成本单价</th>
                            <th style="min-width: 30px; width: 50px">库存量</th>
                            <th style="min-width: 40px; width: 70px">金额</th>
                            <th style="min-width: 50px; width: 50px">操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        <foreach name="list" item="vo" key="k">
                            <tr>
                                <td>
                                    {$vo.parent_name}.{$vo.code_name}
                                    <input type="hidden" class="input" name="code_id_{$k}" value="{$vo.code_id}" >
                                    <input type="hidden" class="input" name="claim_material_id_{$k}" value="{$vo.claim_material_id}" >
                                </td>
                                <td>{$vo.material_name}</td>
                                <td>{$vo.spec}</td>
                                <td>{$vo.prop}</td>
                                <td>
                                    <input type="text" class="input batchNo" name="batch_no_{$k}" value="{$vo.batch_no}" readonly>
                                    <input type="hidden" class="input storage_id" name="storage_id_{$k}" value="{$vo.storage_id}">
                                    <input type="hidden" class="input supplier_id" name="supplier_id_{$k}" value="{$vo.supplier_id}">
                                </td>
                                <td><input type="text" class="input number" name="number_{$k}" value="{$vo.number}" onkeyup="onlyNumber(this)"></td>
                                <td>{$vo.unit}</td>
                                <td><input type="text" class="input price" name="price_{$k}" value="{$vo.cost}" readonly></td>
                                <td><input type="text" class="input number" name="storage_number_{$k}" value="{$vo.storage_number}" readonly></td>
                                <td><input type="text" class="input totalPrice" name="total_price_{$k}" value="{$vo.total_price}" readonly></td>
                                <if condition="$workorder['claim_status'] neq '1' && $workorder['claim_status'] neq '-1'">
                                    <td>
                                        <a class="btn btn-danger btn-del">移除</a>
                                    </td>
                                </if>
                            </tr>
                        </foreach>
                    </tbody>
                </table>
                <if condition="$workorder['claim_status'] neq '1' && $workorder['claim_status'] neq '-1'">
                    <div class="control-group" style="color: #45a1de">
                        编码搜索：
                        <input type="text" class="input" name="codeName" value="">
                        <a class="btn btn-info search">搜索</a>
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        <div class="codeList" style="color: #45a1de;display: none">
                            编码列表：
                            <select name="codeId">
                                <option value='0'></option>
                            </select>
                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        </div>
                        <div class="batchNoList" style="color: #1cc385;display: none">
                            批号列表：
                            <select name="batchNo" style="">
                                <option value='0'></option>
                            </select>
                            <a class="btn btn-success addRow" onclick='javascript:insertRows()' style="display: none">添加</a>
                        </div>
                        <input type="hidden" name="row" value="{$row}"/>
                    </div>
                </if>

                <hr class="half-rule">
                <div class="control-group">
                    <if condition="$workorder['claim_status'] gt '-2'">
                        领料时间：
                        <input type="text" class="input J_date" style="width: 150px" name="claim_date" value="{$workorder.claim_date}" autocomplete="off" readonly>
                        &nbsp;&nbsp;&nbsp;
                        入库人：
                        <select readonly>
                            {:getCommonSelectOptionHtml($claimer['employee_id'], "Admin", "employee_id", "admin_name", null, $user['enterprise_code'], 1)}
                        </select>
                    <else/>
                        领料时间：
                        <input type="text" class="input J_date" style="width: 150px" name="claim_date" value="{$workorder.claim_date}" autocomplete="off">
                        &nbsp;&nbsp;&nbsp;
                        入库人：
                        <select readonly>
                            {:getCommonSelectOptionHtml($user['employee_id'], "Admin", "employee_id", "admin_name", null, $user['enterprise_code'], 1)}
                        </select>
                    </if>
                    <br/><br/>
                    <if condition="$workorder['claim_status'] gt '-2'">
                        审核时间：
                        <input type="text" class="input J_date" style="width: 150px" name="claim_audit_date" value="{$workorder.claim_audit_date}" autocomplete="off">
                        &nbsp;&nbsp;&nbsp;
                        审核人：
                        <select name="claim_auditor" readonly >
                            {:getCommonSelectOptionHtml($user['employee_id'], "Admin", "employee_id", "admin_name", null, $user['enterprise_code'], 1)}
                        </select>
                        &nbsp;&nbsp;&nbsp;
                        审核说明：
                        <input type="text" class="input" style="width: 350px" name="claim_audit_note" value="{$workorder.claim_audit_note}" autocomplete="off">
                    <elseif condition="$workorder['claim_status'] neq ''"/>
                        审核时间：
                        <input type="text" class="input J_date" style="width: 150px" name="claim_audit_date" value="{$workorder.claim_audit_date}" autocomplete="off" readonly>
                        &nbsp;&nbsp;&nbsp;
                        审核人：
                        <select name="claim_auditor" readonly>
                            {:getCommonSelectOptionHtml($claimAuditor['employee_id'], "Admin", "employee_id", "admin_name", null, $user['enterprise_code'], 1)}
                        </select>
                        &nbsp;&nbsp;&nbsp;
                        审核说明：
                        <input type="text" class="input" style="width: 350px;color: #ed7667" name="claim_audit_note" value="{$workorder.claim_audit_note}" autocomplete="off" readonly>


                    </if>
                </div>
                <if condition="$workorder['claim_status'] eq '-1'">
                <div class="control-group">
                    <div class="controls" style="margin-left: 20px">
                        <label class="checkbox inline" style="font-weight: bold">
                            <input type="checkbox" style="zoom: 180%; margin-top: 0.1rem"  name="unAudit"><a style="color: #ed7667;font-weight: bold;font-size: large">拒绝</a>
                        </label>
                    </div>
                </div>
                </if>
            </fieldset>
            <div class="form-actions">
                <php>
                    $btnText = "领料";
                    $btnStatus = -1;
                    $role = explode(',',$user["role"]);
                    if(in_role($role,"8")){
                        if($workorder["claim_status"] == -2 || isNullOrEmpty($workorder["claim_status"])){
                            $btnText = "领料";
                            $btnStatus = 1;
                        }else{
                            $btnText = "已领料";
                            $btnStatus = -1;
                        }
                    }
                    if(in_role($role,"4")){
                        if($workorder["claim_status"] == -1){
                            $btnText = "审核";
                            $btnStatus = 1;
                        }
                        if($workorder["claim_status"] == 1){
                            $btnText = "反审核";
                            $btnStatus = 1;
                        }
                    }
                </php>
                <if condition="$btnStatus eq 1">
                    <button type="submit" class="btn btn-primary btn_submit ">{$btnText}</button>
                    <else/>
                    <button disabled="disable" style="background-color: #93949c" class="btn btn-primary btn_submit">{$btnText}</button>
                </if>
                <input type="hidden" name="id" value="{$workorder.workorder_id}" />
                <input type="hidden" name="btnText" value="{$btnText}"/>
                <a class="btn" href="__URL__">返回</a>
            </div>
        </form>
    </div>
</div>
<script src="__ROOT__/statics/js/common.js"></script>
</body>
</html>